package com.dx.wx.service.impl;

import com.dx.wx.service.DxActService;
import com.dx.wx.service.DxUserService;
import com.dx.wx.util.DxUtil;
import com.ejlchina.data.Array;
import com.ejlchina.data.Mapper;
import com.gc.server.service.UserService;
import com.yf.response.RRException;
import com.yf.utils.DateUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DxActServiceImpl implements DxActService {

    @Resource
    private DxUserService dxUserService;

    @Override
    @SneakyThrows
    public void addAct(Long phone, Map map) {
        Mapper admin = DxUtil.getUserInfo("18588004404"); //统一放到芳姐名下
        String adminId = admin.getString("openUserId");
        Mapper userMapper = dxUserService.findByMobile(String.valueOf(phone));
        String id = (String) map.get("id");
        //连锁
        String linkId = (String) map.get("linkId");
        String linkName = (String) map.get("linkName");
        //产品
        String productNames = (String) map.get("productNames");
        //图片
        List img = (List) map.get("img");
        //骑牛图片
        String qiniuUrls = (String) map.get("imgUrls");
        String startDate = map.get("startDate") + " 00:00:00";
        String endDate = map.get("endDate") + " 00:00:00";
        String remark = (String) map.get("remark");
        Map data = new HashMap();
        //连锁ID
        data.put("linkd__c", linkId);
        //连锁名
        data.put("link__c", linkName);
        //人员信息
        data.put("user_phone__c", phone);
        data.put("top_phone__c", userMapper.getString("field_5h7D5__c"));//上级
        Mapper tem = dxUserService.findByMobile(userMapper.getString("field_5h7D5__c"));
        data.put("top_name__c",tem.getString("field_W1MfE__c"));
        data.put("top_top_phone__c", userMapper.getString("field_rtIpu__c"));//上上级
        tem = dxUserService.findByMobile(userMapper.getString("field_rtIpu__c"));
        data.put("top_top_name__c",tem.getString("field_W1MfE__c"));
        data.put("user_name__c", userMapper.getString("field_W1MfE__c"));
        //产品
        data.put("product__c", productNames);
        data.put("img__c", img);
        data.put("start_date__c", DateUtil.strToDate(startDate));
        data.put("end_date__c", DateUtil.strToDate(endDate));
        data.put("date__c", new Date());
        data.put("status__c", 0);
        data.put("remark__c", remark);
        data.put("img_qiniu__c", qiniuUrls);
        data.put("owner", Arrays.asList(adminId));
        Map base = DxUtil.createMergeData(data);
        base.put("dataObjectApiName", "dx_act__c");
        Mapper mapper;
        Boolean isUpdate = false;
        if(id==null||id.equals("")){
             mapper = DxUtil.save(base);
             id = mapper.getString("dataId");
        }else {
            isUpdate = true;
            DxUtil.update(base);
        }
        addActDetail(id, productNames, adminId,isUpdate);
    }

    @Override
    public Map findById(String id) {
        Map filter = new HashMap();
        filter.put("field_name", "_id");
        filter.put("field_values", id);
        filter.put("operator", "EQ");
        Map data = DxUtil.createSearchData(filter);
        data.put("dataObjectApiName", "dx_act__c");
        Mapper mapper = DxUtil.findOne(data);
        Map res = new HashMap();
        res.put("id", mapper.getString("_id"));
        res.put("linkId", mapper.getString("linkd__c"));
        res.put("linkName", mapper.getString("link__c"));
        res.put("productNames", mapper.getString("product__c").split(","));
        res.put("startDate", timeToStr(mapper.getLong("start_date__c")));
        res.put("endDate", timeToStr(mapper.getLong("end_date__c")));
        res.put("remark", mapper.getString("remark__c"));
        res.put("img", mapper.getString("img__c"));
        if(mapper.getString("img_qiniu__c")!=null){
            res.put("imgUrls", mapper.getString("img_qiniu__c").split(","));
        }
        res.put("imgList", res.get("imgUrls"));
        res.put("status", mapper.getString("status__c"));
        return res;
    }

    private String timeToStr(Long time) {
        Date date = new Date(time);
        DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
        return dateFormat.format(date);
    }

    public void addActDetail(String actId, String productNames, String adminId,Boolean isUpdate) {
        if (isUpdate){//更新的话 要先解绑原来的
            Map se = new HashMap();
            se.put("field_name", "act__c");
            se.put("field_values", actId);
            se.put("operator", "EQ");
            Map dd = DxUtil.createSearchData(se);
            dd.put("dataObjectApiName", "dx_act_detail__c");
            Array array = DxUtil.findSimple(dd);
            array.forEach((index, item) -> {
                Map temMap = new HashMap();
                temMap.put("_id", item.toMapper().getString("_id"));
                //重置岁数产品
                temMap.put("act__c", "");
                Map da = DxUtil.createMergeData(temMap);
                da.put("dataObjectApiName", "dx_act_detail__c");
                DxUtil.update(da);
            });
        }


        if (productNames == null || productNames.equals("")) {
            throw new RRException("请选择产品");
        }
        for (String name : productNames.split(",")) {
            if (name == null || name.equals("")) {
                continue;
            }
            Map map = new HashMap();
            map.put("owner", Arrays.asList(adminId));
            map.put("act__c", actId);
            map.put("product__c", name);
            Map base = DxUtil.createMergeData(map);
            base.put("dataObjectApiName", "dx_act_detail__c");
            DxUtil.save(base);
        }
    }

    @Resource
    private DxUserService userService;

    @Override
    public Array actList(Long phone, Long startDate, Long endDate) {
        Map startDateFilter = null;
        Map endDateFilter = null;
        Map filter = new HashMap();
        Mapper mapper = userService.findByMobile(String.valueOf(phone));
        String role = mapper.getString("field_Rh3vp__c");
        if (role.equals("5")) { //项目总监
            filter.put("field_name", "top_top_phone__c");
        } else if (role.equals("6")) { //区域经理
            filter.put("field_name", "top_phone__c");
        } else {
            //如果是业务员
            filter.put("field_name", "user_phone__c");
        }
//        filter.put("field_name", "user_phone__c");
        filter.put("field_values", phone);
        filter.put("operator", "EQ");
        if (startDate != null && endDate != null) {
            startDateFilter = new HashMap();
            startDateFilter.put("field_name", "date__c");
            startDateFilter.put("field_values", startDate);
            startDateFilter.put("operator", "GTE");
            endDateFilter = new HashMap();
            endDateFilter.put("field_name", "date__c");
            endDateFilter.put("field_values", endDate);
            endDateFilter.put("operator", "LTE");
        }
        Map data = DxUtil.createSearchData(startDateFilter, endDateFilter, filter);
        data.put("dataObjectApiName", "dx_act_detail__c");
        return DxUtil.findSimple(data);
    }


}

